// pages/personnel/index/user/index.js
import Config from "../../../../main.config";
import {getRegisterDetail} from "../../../../service/Personnel";
import {getPersonnelFansList, getPersonnelFocusList} from "../../../../service/Collection";
import {getStorageSync, setStorageSync} from "../../../../utils/Storage";
Component({

    /*
    * 默认接收全局样式
    * */
    options: {
        addGlobalClass: true
    },

    /**
     * 组件的属性列表
     */
    properties: {

    },

    lifetimes: {
        attached: function () {
            this.getRegisterDetail();
            this.getFocusFansNum();
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        _registerInfo: {
            logo: "",
            name: "",
            companyName: "",
        },
        _focusNum: 0,
        _fansNum: 0
    },

    /**
     * 组件的方法列表
     */
    methods: {
        /*
        * 获取员工注册信息
        * */
        getRegisterDetail: async function () {
            try {
                let userId = getStorageSync(Config.storage.userInfo + ".userId");
                let detail = await getRegisterDetail(userId);
                let logoUrl = Config.api.downLoad + "?fileId=" + detail.logo;
                this.setData({
                    "_registerInfo.logo": logoUrl,
                    "_registerInfo.name": detail.name,
                    "_registerInfo.companyName": detail.companyName,
                    "_registerInfo.schoolName": detail.schoolName,
                    "_registerInfo.schooling": detail.schooling,
                    "_registerInfo.grade": detail.grade.split(";")[1].substr(0, 4)
                })
                setStorageSync(Config.storage.registerInfo, detail);
            } catch (e) {
                console.error(e)
            }
        },

        /*
        * 获取关注\粉丝数量
        * */
        getFocusFansNum: async function () {
            try {
                let objectId = getStorageSync(Config.storage.registerInfo + ".id");
                let focus = await getPersonnelFocusList({userId: objectId});
                let fans = await getPersonnelFansList({objectId});
                this.setData({
                    _focusNum: focus.count,
                    _fansNum: fans.count
                })
            } catch (e) {
                console.error(e)
            }
        },
    }
})
